import { Manager } from "./manager.js";
import { Role } from "./parentAir.js"

export class Enemy extends Role{
    constructor(w,h,x,y,imgUrl,boomUrl,speed,hp){
        super(w,h,x,y,imgUrl,boomUrl)
        this.speed = speed;
        this.move();
        this.item.attr('class','enemy')
        this.hp = hp;
    }
    loseHp(hurt){
        if(this.isBoom){
            return;
        }
        this.hp-=hurt;
        if(this.hp <= 0){
            this.boom();
            clearInterval(this.move_itimer);
            let fen = parseInt($('.sco').html());
            if (this.imgUrl == './img/enemy1_fly_1.png'){
                fen += 100;
            }else if(this.imgUrl == './img/enemy2_fly_1.png'){
                fen += 200;
            }else{
                fen += 300;
            }
            $('.sco').html(fen);
            
        }
    }
    move(){
        let _this = this;
        this.move_itimer = setInterval(() => {
            _this.item.offset({'top':_this.y+=_this.speed});
            if(_this.y >= $(window).innerHeight()){
                //从整体的敌人数组中删除掉enemy
                var index =  Manager.allEnemy.indexOf(_this); 
                if (index > -1) { 
                    Manager.allEnemy.splice(index, 1);
                }
                _this.item.remove();
                clearInterval(_this.move_itimer)
            } 
        }, 100/60);
    }
}